/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Frequencia.java
 *
 * Created on 01/08/2010, 18:18:29
 */

package br.leiam.eolica.graficos;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.jfree.chart.ChartPanel;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

import sun.misc.BASE64Encoder;
import br.leiam.eolica.BD.Consulta;

/**
 * 
 * @author Denis
 */
public class CurvaAerogerador extends javax.swing.JPanel {
	private org.jfree.data.jdbc.JDBCXYDataset chartDataset;
	private XYDataset chartDataset2;
	private JFreeChart chart;
	private Consulta consulta;
	private String QUERY = "";
	private String turbinaSelecionada;
	private boolean haveData = false;
	private double[] potencia;
	private double[] velocidadeVentoMedio;
	
	
	public final static String TURBINA_500KW = "500kw";
	public final static String TURBINA_1p5MW = "1.5mw";
	public final static String TURBINA_2p0MW = "2.0mw";
	public final static String TURBINA_2p1MW = "2.1mw";
	

	/** Creates new form Frequencia 
	 * @param altura */
	public CurvaAerogerador(String turbina, int tamx, int tamy) {

		initComponents();
		this.setSize(tamx, tamy);
		turbinaSelecionada = turbina;
		double x = .5;
		double fim = 20;
		for (x = 0; x < fim; x = x + 1) {

			QUERY = QUERY
			+ "select velocidade, potencia_eletrica as potencia from dados_turbina where potencia ='"
			+ turbinaSelecionada + "' and velocidade=" + String.valueOf((int) x)
			+ " union \n";
		}
		QUERY = QUERY
		+ "select 20 as velocidade, potencia_eletrica as potencia from dados_turbina where potencia = '"
		+ turbinaSelecionada + "' and velocidade=20";

		try {
			consulta = new Consulta(QUERY);

			if (consulta.getNumeroDeLinhas() > 0)
				haveData = true;
		} catch (Exception ex) {
			System.out.println(ex.toString());
		}
		int numeroDeLinhas; 
		try {
			numeroDeLinhas = consulta.getNumeroDeLinhas();			
		} catch (NullPointerException e) {
			numeroDeLinhas = 0;
		}
		
		potencia = new double[numeroDeLinhas];
		velocidadeVentoMedio = new double[numeroDeLinhas];
		for (int i = 0; i < numeroDeLinhas; i++) {
			velocidadeVentoMedio[i] = Double.parseDouble(consulta.getValorEm(i,
					0).toString());
			potencia[i] = Double.parseDouble(consulta.getValorEm(i, 1)
					.toString());
			// System.out.printf("\n %d vel = %.4f     freqRel = %f",i,velocidadeVentoMedio[i],frequenciaRelativa[i]);

		}
		chartDataset2 = createDataSet2(velocidadeVentoMedio, potencia);
		chart = org.jfree.chart.ChartFactory.createXYLineChart(
				"Curva de Pot. do Aerogerador", "Velocidade do Vento (m/s)",
				"Potencia (KW)", chartDataset2,
				org.jfree.chart.plot.PlotOrientation.VERTICAL, false, true,
				true);

		ChartPanel chartPanel = new ChartPanel(chart);
		jPanel1.add(chartPanel);
		XYPlot plot = chart.getXYPlot();
		plot.setBackgroundPaint(Color.white);
		plot.setDomainGridlinePaint(Color.gray);
		plot.setRangeGridlinePaint(Color.gray);
		plot.getRenderer(0).setSeriesStroke(0, new BasicStroke(2f));
		// plot.setDomainAxisLocation(AxisLocation.BOTTOM_OR_LEFT);
		// plot.setRangeAxisLocation(AxisLocation.TOP_OR_LEFT);

		// org.jfree.chart.renderer.xy.XYItemRenderer renderer2 = new
		// org.jfree.chart.renderer.xy.XYBarRenderer(.2);

		plot.getRenderer(0).setSeriesPaint(0, Color.blue);
		// renderer2.setSeriesShape(0, null);
		// renderer2.setSeriesStroke(0, null);

		// renderer2.setSeriesPaint(0, Color.red);
		// plot.setRenderer(0, renderer2);

		chartPanel.setSize(tamx, tamy);

		this.setSize(tamx, tamy);
		jPanel1.setSize(tamx, tamy);

	}

	private XYDataset createDataSet2(double[] velVento, double[] Fv) {
		XYSeries serie1 = new XYSeries("Frequencia");

		for (int i = 0; i < velVento.length; i++) {
			serie1.add(velVento[i], Fv[i]);

		}
		XYDataset xyDataset = new XYSeriesCollection(serie1);
		return xyDataset;
	}

	public String getBase64String() {
		String figura = "";
		this.setVisible(true);

		try {
			double escalaX = 1, escalaY = 1;
			Component co = this;
			BufferedImage bi = new BufferedImage(
					(int) (co.getSize().getWidth() * escalaX), (int) (co
							.getSize().getHeight() * escalaY),
					BufferedImage.TYPE_INT_RGB);
			Graphics2D g2d = bi.createGraphics();
			g2d.scale(escalaX, escalaY);
			co.paint(g2d);

			byte[] figBytes;

			figBytes = ChartUtilities.encodeAsPNG(bi);

			BASE64Encoder encoder = new BASE64Encoder();
			figura = encoder.encode(figBytes);
		} catch (Exception ex) {
			Logger.getLogger(Weibull.class.getName()).log(Level.SEVERE, null,
					ex);
		}
		return figura;
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed"
	// desc="Generated Code">//GEN-BEGIN:initComponents
	private void initComponents() {

		jPanel1 = new javax.swing.JPanel();

		jPanel1.setLayout(new java.awt.BorderLayout());

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
		this.setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addComponent(
				jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 408,
				Short.MAX_VALUE));
		layout.setVerticalGroup(layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addComponent(
				jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 324,
				Short.MAX_VALUE));
	}// </editor-fold>//GEN-END:initComponents

	// Variables declaration - do not modify//GEN-BEGIN:variables
	private javax.swing.JPanel jPanel1;

	// End of variables declaration//GEN-END:variables

	/**
	 * @return the haveData
	 */
	public boolean isHaveData() {
		return haveData;
	}

	/**
	 * @param haveData
	 *            the haveData to set
	 */
	public void setHaveData(boolean haveData) {
		this.haveData = haveData;
	}

	/**
	 * @return the velocidadeVentoMedio
	 */
	public double[] getVelocidadeVentoMedio() {
		return velocidadeVentoMedio;
	}

	/**
	 * @param velocidadeVentoMedio
	 *            the velocidadeVentoMedio to set
	 */
	public void setVelocidadeVentoMedio(double[] velocidadeVentoMedio) {
		this.velocidadeVentoMedio = velocidadeVentoMedio;
	}

	/**
	 * @return the potencia
	 */
	public double[] getPotencia() {
		return potencia;
	}

	/**
	 * @param potencia
	 *            the potencia to set
	 */
	public void setPotencia(double[] potencia) {
		this.potencia = potencia;
	}

}
